package com.htjx.mall.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.cart.CartItem;
import com.htjx.mall.controllers.commonapi.Cart;
import com.htjx.mall.controllers.commonapi.CommonCookie;
import com.htjx.mall.services.ProductService;

@Controller
@RequestMapping("/cart")
public class CartController {
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result list(HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "查询成功");
		result.setBody(cart);
		return result;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result deleteAll(HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		cart.removeAll();
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "添加成功");
		return result;
	}
	
	@RequestMapping(value = "/item", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result getItem(@RequestParam("specification_id")long specificationId, HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		
		CartItem cartItem = cart.getItem(specificationId);
		if(cartItem == null) {
			return new Result(9, "无法修改购物车");
		}
		
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "添加成功");
		result.setBody(cartItem);
		return result;
	}
	
	@RequestMapping(value = "/item/update", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result updateItem(@RequestParam("specification_id")long specificationId, @RequestParam("quantity") int quantity, HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		cart.updateItem(specificationId,quantity);
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "更新成功");
		return result;
	}
	
	@RequestMapping(value = "/item/delete", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result deleteItem(@RequestParam("specification_id")long specificationId, HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		cart.remove(specificationId);
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "删除成功");
		return result;
	}
	
	@RequestMapping(value = "/item/add", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result add(@RequestParam("specification_id")long specificationId, @RequestParam("quantity") int quantity, HttpServletRequest request, HttpServletResponse response) {
		Cart cart = CommonCookie.getCartFromCookie(request);
		CartItem cartItem = productService.fillCartItem(specificationId);
		cartItem.setQuantity(quantity);
		cartItem.setActive(true);
		cart.add(cartItem);
		CommonCookie.setCartToCookie(cart, response);
		Result result = new Result(0, "添加成功");
		return result;
	}
}
